//
//  KZSearchBar.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/26.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

import RxSwift

class KZSearchBar: UIView {
    
    let rightButton = UIButton()
    let searchTextField = UITextField()
    
    let bag = DisposeBag()

    convenience init(searchClouse: @escaping (String) -> (), cancelClouse: @escaping () -> ()) {
        self.init(frame: .zero)
        self.rightButton.setTitle("取消", for: .normal)
        self.rightButton.rx.tap.bind {
            [weak self] in
            if self!.rightButton.titleLabel!.text == "取消" {
                cancelClouse()
            } else {
                searchClouse(self!.searchTextField.text!)
            }
            
        }.disposed(by: bag)

        setupUI()
    }
    
    

}

// MARK: ------ 创建视图
extension KZSearchBar {
    
    func setupUI() {
        
        self.addSubview(rightButton)
        rightButton.setTitleColor(UIColor.gray, for: .normal)
        rightButton.titleLabel?.font = kFont(15)
        rightButton.snp.makeConstraints { (m) in
            m.top.right.bottom.equalTo(0)
            m.width.equalTo(80)
        }
        
        let bar = UIView()
        bar.backgroundColor = UIColor.init(white: 0.92, alpha: 1)
        bar.layer.cornerRadius = 15
        bar.layer.masksToBounds = true
        self.addSubview(bar)
        bar.snp.makeConstraints { (m) in
            m.left.equalTo(20)
            m.right.equalTo(rightButton.snp.left)
            m.centerY.equalTo(self)
            m.height.equalTo(30)
        }
        
        let searchIconView = UIImageView()
        searchIconView.image = #imageLiteral(resourceName: "搜索-搜索icon")
        bar.addSubview(searchIconView)
        searchIconView.snp.makeConstraints { (m) in
            m.left.equalTo(15)
            m.width.height.equalTo(15)
            m.centerY.equalTo(bar)
        }
        
        let cancelIconView = UIImageView()
        cancelIconView.image = #imageLiteral(resourceName: "搜索-叉icon")
        bar.addSubview(cancelIconView)
        cancelIconView.snp.makeConstraints { (m) in
            m.right.equalTo(-15)
            m.width.height.equalTo(17)
            m.centerY.equalTo(bar)
        }
        
        self.addSubview(searchTextField)
        searchTextField.placeholder = "搜索商品关键词"
        searchTextField.font = kFont(14)
        searchTextField.delegate = self
        searchTextField.textColor = kThemeColor
        searchTextField.snp.makeConstraints { (m) in
            m.left.equalTo(searchIconView.snp.right).offset(15)
            m.right.equalTo(cancelIconView.snp.left).offset(-15)
            m.top.bottom.equalTo(0)
        }
   
        
        
    }
}

extension KZSearchBar: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        let text = NSMutableString(string: textField.text!)
        text.replaceCharacters(in: range, with: string)

        print(text)
        if text.length == 0 {
            rightButton.setTitle("取消", for: .normal)
        } else {
            rightButton.setTitle("搜索", for: .normal)
        }
        return true
    }
}


